home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-10-27 | 50.3 KB | 1,049 lines |
- Spellcaster presents:
-
-
- TTTTTTTTTT HH HH EEEEEEEEEE MM MM AAAA GGGGGGGGG
- TT HH HH EE MMM MMM AA AA GG
- TT HH HH EE MM M M MM AA AA GG
- TT HHHHHHHHHH EEEEEE MM MM MM AAAAAAAA GG
- TT HH HH EE MM MM AA AA GG GGGG
- TT HH HH EE MM MM AA AA GG GG
- TT HH HH EEEEEEEEEE MM MM AA AA GGGGGGGG
-
- Issue 1
- 25-9-95
-
-
-
- ■ Index
-
- 1. Introduction
- 1.1. About the magazine
- 1.2. About the author
- 1.3. Distribution
- 1.4. Contribuitions
- 1.5. Hellos and greets
- 2. Hexadecimal and binary - The untold story
- 2.1. What are they ?
- 3. Bits and bytes - To they really bite ?
- 3.1. What are they ?
- 3.2. The relatives
- 4. Introduction to Pascal
- 4.1. What is Pascal
- 4.2. The IDE
- 4.3. How to build a program in Pascal - Your first program
- 4.4. Variables
- 4.5. The Writeln and Readln keywords
- 5. Our friend, the pointer - Part I
- 5.1. What the hell is a pointer ?
- 5.2. What's the use ?
- 5.3. How can I use a pointer ?
- 6. Points of view
- 7. The adventures of Spellcaster, part 1.
-
-
- ■ Introduction
-
- ■ About the magazine
-
- Welcome to the first issue of 'The Mag', the electronic magazine, brought
- to you by Spellcaster, alias Diogo de Andrade. Ok, ok, I know the magazine has
- a crapy name, but it was the only thing I could think of.
- This magazine is dedicated to all the programmers and would-be programmers
- out there, to those who wish to learn how to program anything, from demos to
- games, passing through utilities and all sort of thing your mind can think of.
-
- When you read this magazine, I'll assume some things. First, I assume you
- have Borland's Turbo Pascal, version 6 and upwards. I'll also think you have
- a 80386 (or 386 for short; a 486 would be even better), a load of patience
- and a sense of humor. This last is almost essencial, because I don't receive
- any money for doing this, so I must have fun doing it. I will also take for
- certain you have the 9th grade (or equivelent).
-
- As I stated above, this magazine will be made especially for those who don't
- know where to get information, or want it all in the same place, and to those
- who want to learn how to program, so I'll try to build knowledge, building up
- your skills issue by issue. If you sometimes fail to grasp some concept, don't
- despair; try to work it out.
- That's what I did... Almost everything I know was learnt from painfull
- experience. If you re-re-re-read the article, and still can't understand it,
- just drop me a line, or just plain forget it. Most of the things I try to
- teach here aren't linked to each other (unless I say so), so if you don't
- understand something, skip it and go back to it some weeks later. It should
- be clearer for you then. Likewise, if you see any terms or words you don't
- understand, follow the same measures as before.
-
- Ok, as I'm earing the Net gurus and other god-like creatures talking
- already, I'm just going to explain why I use Pascal.
- For starters, Pascal is a very good language, ideal for the beginner, like
- BASIC (yech!), but it's powerfull enough to make top-notch programms.
- Also, I'll will be using assembly language in later issues, and Pascal makes
- it so EASY to use.
- Finally, if you don't like my choice of language, you can stop whining. The
- teory behind each article is very simple, and common with any of the main
- languages (C, C++, Assembly - Yes, that's true... BASIC isn't a decent
- language).
-
- Just one last thing... The final part of the magazine is a little story
- made up by my distorted mind. It's just a little humor I like to write, and
- it hasn't got nothing to do with programming (well, it has a little), but,
- as I said before, I just like to write it.
-
- ■ About the author
-
- Ok, so I'm a little egocentric, but tell me... If you had the trouble of
- writing hundreds of lines, wouldn't you like someone to know you, even by
- name ?
-
- My name is Diogo de Andrade, alias Spellcaster, and I'm the creator,
- editor and writer of this magazine.
- I live in a small town called Setúbal, just near Lisbon, the capital of
- Portugal... If you don't know where it is, get an encyclopedia, and look for
- Europe. Then, look for Spain. Next to it, there's Portugal, and Setúbal is in
- the middle.
-
- I'm 18 years old, and I just made it in to the university (if you do want
- to know, I'm in the Technical Institute of Lisbon, Portugal), so I'm not
- a God-Like creature, with dozens of years of practice (I only program by
- eight years now), with a mega-computer (I own a 386SX, 16 Mhz), that wear
- glasses with lens that look like the bottom of a bottle (I use glasses, but
- only sometimes), that has his head bigger than a pumpkin (I have a normal
- sized head) and with an IQ of over 220 (mine is actually like 180).
- I can program in C, C++, Pascal, Assembly and even BASIC (yech!).
-
- So, if I am a normal person, why do I spend time writing this ?
- Well, because I have the insane urge to write thousands of words every now
- and then, and while I'm at it, I may do something productive, like teaching
- someone. I may be young, but I know a lot about computers (how humble I am;
- I know, modesty isn't one of my qualities).
-
- Just one more thing, if you ever program anything, please send to me... I
- would love to see some work you got, maybe I even could learn something with
- it. Also, give me a greet in your program/game/demo... I love seeing my
- name.
-
- ■ Distribution
-
- I don't really know when can I do another issue, so, there isn't a fixed
- space of time between two issues. General rule, I will try to do one every two
- weeks, maybe more or maybe less.
- 'The Mag' is available by the following means:
-
- - Snail Mail : My address is below, in the Contributions seccion... Just
- send me a disk and tell me what issues you want, and I
- will send you them...
-
- - E-Mail : If you E-mail me and ask me for some issues, I will Email you
- back with the relevant issues attached.
-
- - BBS's : I don't know for sure what BBS's have or will have my magazine,
- but I will try to post it in the Skyship BBS.
- If you have a BBS and you want to receive 'The Mag', contact me.
-
- Skyship BBS numbers: (351)+01-3158088
- (351)+01-3151435
-
- By the end of this year (1995), I should have an Internet Page, and some
- more BBS's in my list, besides some ftp's.
-
- ■ Contributions
-
- I as I stated before, I'm not a God... I do make mistakes, and I don't
- have (always) the best way of doing things. So, if you think you've spotted
- an error, or you have thought of a better way of doing things, let me know.
- I'll be happy to receive anything, even if it is just mail saying 'Keep it
- up'. As all human beings, I need incentive.
-
- Also, if you do like to write, please do... Send in articles, they will be
- welcome, and you will have the chance to see your names up in lights.
-
- If anyone out there has a question or wants to see an article about
- something in particular, feel free to write... All letters will be answered,
- provided you give me your address.
-
- Also, I'm looking for people to create a new demo/game/utilities group, and
- I need all sort of people, from coders (sometimes, one isn't enough),
- musicians (I can compose, but I'm a bit limited), graphics artists (I can't
- draw nothing) and spreaders... I mean, by a spreader, someone who spreads
- things, like this mag. If you have a BBS and you want it to include this
- magazine (or anything that I make), feel free to write me...
-
- You can also contact me personally, if study on the IST (if you don't
- know what the IST is, you don't study there). I'm the freshman with the
- black hair and dark-brown eyes... Yes, the not-so-tall but pretty one (there
- goes my modesty again). I recommend you to contact me personally, if you can,
- especially if you are a member of the opposite sex (I'm a man, for those of
- you who are wondering).
-
- My adress is:
- Praceta Carlos Manito Torres, nº4/6ºC
- 2900 Setúbal
- Portugal
-
- Email: dgan@rnl.ist.utl.pt
-
-
- ■ Hellos and greets
-
- I'll say hellos and thanks to all my friend, especially for those who put
- up with my constant whining (you know who you are).
- Special greets go to Denthor from Asphyxia (for the excelent VGA trainers),
- Draeden from VLA (for assembly tutorials), Joaquim Elder Guerreiro, alias
- Dr.Shadow (Delta Team is still up), Alex "Darkfox" (thanks for letting me
- use your BBS), Joäo Neves and Henrique Craveiro for sugestions, and all the
- demo groups out there.
- I also want to say hi to my idols (I know they don't read this, but...),
- Chris Roberts, François Lionet, Archer MacLean, everybody at ID Software and
- Apogee, Sierra On-Line, Lucas Arts and Team 17, for showing me what
- programming is all about.
-
-
-
-
- ■ Hexadecimal and binary - The untold story
-
- This article is about hexadecimal and binary. This is a purely teorical
- article, so, if you know everything about them, go on to the next part.
-
- ■ What are they ?
-
- Hexadecimal (Hex for short) and binary are diferent numbering sistems. An
- more common example of a numbering sistem is the decimal sistem, that we use
- everyday. The teory behind decimal is that there are 10 digits, starting from
- 0 up to 9. If you join the digits together, you will get the numbers. This is
- called a base 10 sistem. The hexadecimal sistem is a base 16 sistem. This
- means you have the digits from 0 to 9, but after 9 you still have A, B, C,
- D, E and F. Don't worry if this sound a little weird. It will be a lot
- simpler when you look at the next chart. The first column is the number in
- decimal, the other one is the hexadecimal correspondent.
-
- ┌─────────────┬──────────────┐ ┌─────────────┬──────────────┐
- │ DECIMAL │ HEX │ │ DECIMAL │ HEX │
- ├─────────────┼──────────────┤ ├─────────────┼──────────────┤
- │ 0 │ 0 │ │ 16 │ 10 │
- │ 1 │ 1 │ │ 17 │ 11 │
- │ 2 │ 2 │ │ 18 │ 12 │
- │ 3 │ 3 │ │ 19 │ 13 │
- │ 4 │ 4 │ │ 20 │ 14 │
- │ 5 │ 5 │ │ 21 │ 15 │
- │ 6 │ 6 │ │ 30 │ 1F │
- │ 7 │ 7 │ │ 31 │ 20 │
- │ 8 │ 8 │ │ 40 │ 28 │
- │ 9 │ 9 │ │ 50 │ 32 │
- │ 10 │ A │ │ 60 │ 3C │
- │ 11 │ B │ │ 70 │ 46 │
- │ 12 │ C │ │ 80 │ 50 │
- │ 13 │ D │ │ 100 │ 64 │
- │ 14 │ E │ │ 200 │ C8 │
- │ 15 │ F │ │ 255 │ FF │
- └─────────────┴──────────────┘ └─────────────┴──────────────┘
-
- Do you understand it now ? I'll teach you how to convert from one base to
- another in a second. Another thing you must know, is that that hexadecimal
- number is almost everytime acompained with the '$' simbol or with the letter
- 'h' in the end of it. So, $10 is the same that 10h, and it means 16 in
- decimal.
-
- But how do you convert from a base to another, I hear you cry. It's easier
- explain with an example.
- Let's convert the number 255 decimal to hexadecimal (I know by experience
- that 255 decimal is FFh or $FF).
- You divide the 255 by 16. Then,
- 255 │ 16 you convert the quocient to
- 15 └───── hexadecimal (in this case it equals
- 15 F ) and then convert the reminder
- (again it's F). So you finish with
- $FF. Let's try again...
-
- Convert the number 654 to hex... This time it's more tricky, but try for
- yourself, then check the result.
-
- 654 │ 16
- 14 = Eh <-- 14 └─────┐
- 40 │ 16 So, 654 = 28Eh
- 8 └─────
- 2
-
-
- Now, let's try to do the opposite, that is, convert from hexadecimal to
- decimal. This is easier to do. All you have to do is to know the powers of
- 16. Let's convert FFh to decimal (you already should know this is equal to
- 255).
-
- FF = 15 * 16 + 15 = 255 ; Probably, you didn't understood it,
- so I'll explain it better.
-
- 16^3 16^2 16^1 16^0 ; These are the powers of 16.
- 4096 256 16 1
-
- 0 0 F F ; this is the number we want to convert
- 00FFh = FFh
- Now, we multiply the second F (counting
- from the right) by 16.
- F*16 = 15*16 = 240; and then you add the
- other F; 240+F = 240+15 =255.
-
- See, how it was easy ?! Let's try with another number...
- Let's convert 28Eh to decimal.
-
- 16^3 16^2 16^1 16^0
- 4096 256 16 1
-
- 0 2 8 E
-
- So, we get: 256*2 + 8*16 + E = 512 + 128 + 14 = 654 !!!
- I know this sound complicated for a begginner, but it will all sound clearer
- in time. Don't be scared by it... Don't let it prevent you from making
- programming masterpieces. Some of these things aren't usefull, for now, but
- they'll proove invaluable in the future, so I decided that it would be better
- if you learned it now.
-
- So, the article's about hexadecimal and BINARY... So, let's talk binary for
- now. Has you probably has guessed, binary is another numbering sistem, this
- time a base 2 sistem... Yep, that's right a sistem that only has two digits,
- the 0 and the 1. So, let's do another table...
-
- ┌─────────────┬──────────────┐ ┌─────────────┬──────────────┐
- │ DECIMAL │ BINARY │ │ DECIMAL │ BINARY │
- ├─────────────┼──────────────┤ ├─────────────┼──────────────┤
- │ 0 │ 00000000 │ │ 16 │ 00010000 │
- │ 1 │ 00000001 │ │ 17 │ 00010001 │
- │ 2 │ 00000010 │ │ 18 │ 00010010 │
- │ 3 │ 00000011 │ │ 19 │ 00010011 │
- │ 4 │ 00000100 │ │ 20 │ 00010100 │
- │ 5 │ 00000101 │ │ 21 │ 00010101 │
- │ 6 │ 00000110 │ │ 30 │ 00011110 │
- │ 7 │ 00000111 │ │ 31 │ 00011111 │
- │ 8 │ 00001000 │ │ 40 │ 00101000 │
- │ 9 │ 00001001 │ │ 50 │ 00110010 │
- │ 10 │ 00001010 │ │ 60 │ 00111100 │
- │ 11 │ 00001011 │ │ 70 │ 01000110 │
- │ 12 │ 00001100 │ │ 80 │ 01010000 │
- │ 13 │ 00001101 │ │ 100 │ 01100100 │
- │ 14 │ 00001110 │ │ 200 │ 11001000 │
- │ 15 │ 00001111 │ │ 255 │ 11111111 │
- └─────────────┴──────────────┘ └─────────────┴──────────────┘
-
- So, this is fairly simple, isn't it ?!
- Let's do some conversion... This is just like the decimal to hexadecimal
- conversion, but you divide by two...
-
- 255 │ 2
- 1 └────┐
- 127 │ 2
- 1 └────┐
- 63 │ 2
- 1 └────┐
- 31 │ 2
- So, 255 = 11111111b 1 └────┐
- | 15 │ 2
- \|/ 1 └────┐
- This is the 7 │ 2
- same as the 'h' in 1 └────┐
- hexadecimal... 3 │ 2
- 1 └────
- 1
-
- See, how easy this is... And converting from binary to decimal is as
- easy as PI = 3.141592654. In this case, you must know the powers of two.
-
-
- 2^7 2^6 2^5 2^4 2^3 2^2 2^1 2^0
- 128 64 32 16 8 4 2 1
-
- 1 1 1 1 1 1 1 1
-
- 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255
-
- So, we come to the breath-taking conclusion that 255 = 11111111b...
- Can you believe it ? Another example: converting 10010001b.
-
- 2^7 2^6 2^5 2^4 2^3 2^2 2^1 2^0
- 128 64 32 16 8 4 2 1
-
- 1 0 0 1 0 0 0 1
-
- 128 + 16 + 1 = 145
-
- You have now taken the first steps into a larger world... The cyber-world.
- You know what hexadecimal and binary are... I'll let it to you to discover
- how to convert from hexadecimal to binary and vice-versa... It's also
- easy... Trust me on this.
-
- To those super-programmers how were reading this just to know the untold
- story of hexadecimal and binary, you are in for a surprise... There isn't
- an untold story !!! That's all folks !...
-
-
-
-
- ■ Bits and bytes - Do they really bite ?
-
- No they don't, answering to the pending question. Let's delve into the
- world of bits and bytes, and know all about their family.
-
- ■ What are they ?
-
- You must know something about computers, as you read this article.
- Computers are very stupid... They can only understand something: 0's and
- 1's. You may be asking to yourselfs 'What is he talking about ?'.
- For starters, the computer only operates with 0's and 1's (active and
- desactive, on and off, true of false). In the dawn of computers, everything
- was made by a neverending serie of 1's and 0's. This is known as a bit.
- The bit is either 0 and 1 (sound's familiar with binary). The computer can
- then perform a series of operations on them, but now, this is unimportant.
- That's why the binary notation is very important in computers.
-
- As you may have guessed, the computer wouldn't be very usefull if it could
- only manage 0's and 1's. So the byte was invented. The byte is a serie of
- bits join together (8 of them). As a byte has 8 bits, it can store values
- that go from 00000000b to 11111111b (that is, from 0 to 255). To make this
- clearer with this example:
-
- Imagine a switch: ┌───┐
- │ │ <-- this is a switch.
- └───┘
- Now, a any switch, it can be on or off:
-
- ┌───┐ ┌───┐
- │ 0 │<-- This switch is off │ 1 │<-- This switch in on
- └───┘ └───┘
-
- This is a bit... A byte is just a serie of 8 switches, some of them are on,
- some of them are off. So:
-
- ┌───┐┌───┐┌───┐┌───┐┌───┐┌───┐┌───┐┌───┐
- │ 0 ││ 1 ││ 0 ││ 1 ││ 1 ││ 0 ││ 0 ││ 1 │ -> This is a byte, that
- └───┘└───┘└───┘└───┘└───┘└───┘└───┘└───┘ holds the number 89.
-
- Ok, so now we know waht a bit and a byte are.
-
- ■ The relatives
-
- Like every unit, the bit and the byte have multiples.
-
- The middle brother of the byte family is the word. The word is equal to
- two bytes. So now, you can store numbers that range from 0 to 65535,
- because you have another byte to store things.
-
- The oldest brother is the Double Word, or Dword. As you probably guessed,
- this is two words together, or four bytes. This gives you a very big number
- to store... I think is from 0 to 4.294.967.295, give and take some.
-
- Next you have the father of byte, word and Dword: Mr.Kilobyte, or Kb for
- short. This is equal to 1024 bytes (not 1000 as the name sugests). This
- measure used to say the capacity of a disk or memory.
-
- But, even in the byte family there is the grandfather: Grandpa Megabyte,
- also known as Mb and Mo. Well this is equal to 1024 Kb, or 1.048.576 bytes.
-
- Finally, you have the grand-grandfather... He is called Gigabyte and he
- is very big... It has 1024 Mb, so that a whole lot of bytes...
-
- So, now you know all about the byte family, from the humble byte to the
- mighty gigabyte. I know this sound a bit strange, but it will soon grow
- within you. As I said before, you musn't be desmotivated by it, because most
- of these things will become part of your knowledge with time. It will become
- like your second language (ok, like your third, if you are portuguese and
- you know english). Soon you will be playing with them without trouble.
-
-
-
-
- ■ Introduction to Pascal
-
- ■ What is Pascal
-
- Technically, Pascal is a high-level programming language. Poetically, it
- is the tool that makes dreams come true...
- What Pascal really is is a translator. It converts 'sentences' that
- ressemble a normal talking and writing language to binary codes the computer
- can execute. This process is know as compiling.
- It was invented in the seventies (I think) by a professor that wanted a
- language that was easy to learn (as BASIC), yet powerfull.
- You must be thinking what differs from Pascal and BASIC (by now, you must
- have realised that I hate BASIC). Well, the difference is that BASIC is an
- interpreter, while Pascal is a compiler. A interpreter functions like this:
-
- ┌─> Get untranslated command
- │ Translate command
- └── Execute translated command
-
- The compiler works like this:
-
- Translate all commands
- ┌─> Get translated command
- └── Execute translated command
-
- It doesn't take an Einstein to figure out that a compiler is faster than
- a interpreter. There's all sort of little diferences beetween them, appart
- from this, but they are all about restrictions. General rule is the
- interpreter is more limited than a compiler. The interpreter is easier to
- understand, that is true, but it's very limited all the same.
-
- ■ The IDE
-
- It's a bit weird for me teaching the fundamental about Pascal. There's
- so much to teach, and I don't know where to start. I'll start with the editor.
- When you first startup Pascal, (by typing 'turbo' or something like it in
- the DOS), you will get a blue window, with a white frame, with a series of
- menus in the toolbar above it. Don't worry if the window doesn't appear. Just
- press ALT+F. The File menu will expand. Select then the New option, either
- with the arrow keys and Return, or pressing the 'N' key.
- You are now looking to the IDE... The Integrated Development Enviroment.
- This is where you are going to write your future masterpieces. I'll teach
- you how to do things as we progress throughout the magazine.
-
- ■ How to build a program in Pascal - Your first program
-
- Every program in Pascal must start with a phrase like:
-
- PROGRAM myprogram(input,output); or
- PROGRAM myprogram(output); or
- PROGRAM myprogram(input); or
-
- This isn't really necessary (with Pascal 6 and upwards), but it's good
- programming practice to put it there. The word 'PROGRAM' is a keyword.
- A keyword is a word that Pascal recognizes (if you use Pascal V7.0, it
- will turn white, as opposing the rest of the yellow text). As I was
- saying, 'PROGRAM' is a keyword that says to the compiler the name of the
- program (in this cases, the name is myprogram), and what kind of program
- it is. In the first case, the program is (input,output), that is, the
- program receives and writes data. By data, I mean any kind of data, is it
- key, joystick or mouse commands, images, sounds, etc... Data can be anything
- at all you want. In the second case, the program only outputs, that is, it
- only gives out data. In the third case, you have an unusual case, seldom
- used. It is the input only program. That only receives data.
- Also notice the semi-colon in the end of the 'phrase'. It is necessary you
- write it. Almost every command in Pascal ends with a ';'. At first you will
- probably forget to put it, but when you run a program (that is, when you
- compile it), Pascal will warn about it and will not run the program. As I
- teach you new commands, I will tell which ones don't need the semi-colon. If
- I don't say nothing about it, assume the command needs it.
- The next thing in the program is the keyword 'BEGIN'. This tells the
- compiler that a block of instructions follows. There are 5 kinds of
- instruction blocks, and I will talk about all of them, in time. For the
- meanwhile, in this case, this 'BEGIN' tells us that the main program block
- starts there. Notice: For every 'BEGIN', there's and 'END', that is, the
- last line in your program should be 'END.'. Also notice that the 'BEGIN'
- keyword is never followed by a semi-colon, while the 'END' does, except if
- it is the end of the program, in which case it is followed by a dot.
- Let's see an example:
-
- Program Do_Nothing; { you can omit the (input,output) }
-
- Begin
- End.
-
- This program would compile, but it wouldn't do nothing.
- Another thing to know about a Pascal program is that the compiler ignores
- anything that is enclosed by '{' and '}'. So, you can write comments on the
- program, just make sure you open and close the brackets.
- Ok, let's make a program that actually makes something...
-
- Program Hello(output); { This is the title of the program, }
- { that only outputs... }
- Begin { Begining the main block }
- Writeln('Hello everybody'); { Writes 'Hello everybody' in the screen}
- Readln; { Waits for RETURN to be pressed }
- End. { Ends the main block and exits program}
-
- Type in this code (another name for the program) and run it. To run the
- program, press ALT+R. The Run menu will pop up. Press 'R' now, and the program
- will compile. Then it will write in the screen 'Hello everybody'. Then it will
- wait for you to press RETURN and it will return to the IDE. Another way to
- run the program is to press CTRL+F9.
- About the 'WRITELN' and 'READLN' keywords, I will refer to them later on.
- Now, let's save your program. Go to the File menu (ALT+F) and then press
- 'S'. A file browser will appear. Write the filename of your program (something
- like 'HELLO') and press RETURN. The program will be saved. If you didn't
- specified the extension, Pascal will assume '.PAS'. I recommend you to make
- a directory to hold your Pascal programs, so you always know were they are.
-
- I know you must be thinking: 'Ow ! Big deal, I made a sentence appear in
- the screen'. I know this isn't impressive, but it's a beginning. At first
- this is off-putting, but as you progress you will see better results.
-
- I tought I'd stop for now, but I hear you cry: 'More! We want more!'. So
- here it goes...
-
- ■ Variables
-
- This is one of the more important things in programming, so pay attencion.
- A variable is a place where you can store data of various types.
- A variable (also called a var) in Pascal must be defined before the
- beginning of the main block of your program. Here's a list of some of the
- types of variables there exists, alongside with their description.
-
- ┌───────────────────┬────────────────────────────────────────────────────┐
- │ Type │ Description │
- ├───────────────────┼────────────────────────────────────────────────────┤
- │Shortint │A signed integer number (a number that can be │
- │ │positive and negative) that ranges from -128 to 127 │
- │ │ │
- │Integer │A signed integer number that ranges from -32768 to │
- │ │32767 │
- │ │ │
- │Longint │A signed integer number that ranges from -2147483648│
- │ │to 2147483647 (very big!) │
- │ │ │
- │Byte │An unsigned integer number that ranges from 0 to 255│
- │ │ │
- │Word │An unsigned integer number that ranges from 0 to │
- │ │65535 (sounds familiar ?!) │
- │ │ │
- │Char │A simple character... Think of it as a letter... │
- │ │ │
- │String │A collection of Char's... It's like a sentence. │
- └───────────────────┴────────────────────────────────────────────────────┘
-
- These are only some of the types of vars that exist in Pascal... There
- are many others and you can even define new ones, but that's something I'll
- talk about in many issues from now...
-
- You are probably wondering what the hell is that all about... Let me
- demonstrate with a series of example programs... They are all commented, so
- you will probably understand them.
-
- Program Test_1; { This starts the program Test_1 }
- { This program shows you how to give define and give }
- { values to variables }
-
- Var A:Integer; { This defines a var of type Integer and calls it A }
- B:Word; { This defines a var of type Word and calls it B }
- C:Char; { This defines a var of type Char and calls it C }
- D:String; { This defines a var of type String and calls it D }
-
- Begin
- A:=0; { This gives var A the value 0 }
- B:=50; { This gives var B the value 50 }
- C:='B' { This stores in var C the letter B }
- D:='Hello' { This stores in var D the word 'Hello' }
- Writeln(A); { This writes to the screen the content of var A }
- Writeln(B); { This writes to the screen the content of var B }
- Writeln(C); { This writes to the screen the content of var C }
- Writeln(D); { This writes to the screen the content of var D }
- Readln; { This waits for RETURN to be pressed }
- End. { This ends the program }
-
-
-
- Program Test_2; { This starts the program Test_2 }
- { This program shows you how to do some operations with}
- { variables }
-
- Var Num1:integer; { This defines a var of type Integer and calls it Num1 }
- Num2:Word; { This defines a var of type Word and calls it Num2 }
- Word1:String; { This defines a var of type String and calls it Word1 }
- Word2:String; { This defines a var of type String and calls it Word2 }
-
- Begin
- Num1:=15; { This gives var Num1 the value 15 }
- Num2:=50; { This gives var Num2 the value 50 }
- Word1:='Hello'; { This stores in var Word1 the word 'Hello' }
- Word2:='World'; { This stores in var Word2 the word 'World' }
- Num1:=Num1+Num2; { This adds Num1 to Num2 and places the result in Num1 }
- Word1:=Word1+Word2; { This adds Word1 to Word2 and places the result in }
- { Word1 }
- Writeln(A); { This writes to the screen the content of var A }
- Writeln(B); { This writes to the screen the content of var B }
- Writeln(C); { This writes to the screen the content of var C }
- Writeln(D); { This writes to the screen the content of var D }
- Readln; { This waits for RETURN to be pressed }
- End. { This ends the program }
-
- Did you understand it ? It's quite easy when you get the hang of it. Play
- with the programs and you'll see how it works.
- You can execute all sort of operations with variables, and even include
- more than one operation at a time. They are very flexible, but notice a few
- things:
-
- - If you have a var of type Char and you give the value 'ALFA', this is the
- same that giving that variable the value 'A', because the var is type Char,
- that only handles one character.
-
- - When you want to give a value to a variable, you must include ':' after it.
- If you don't, you will get an error message.
-
- A:=10; { This is ok }
- A=10; { this wouldn't compile }
-
- - You can specify the maximum number of characters a string can hold,
- putting the maximum number of characters (chars for short) it can hold
- in front of it, enclosed in square braquets.
-
- Var S:String[14]; { This defines the string S, with 14 chars }
-
- Then, when you write something like:
-
- S:='SPELLCASTER RULES'
-
- It would be 'clipped', that is, the variable S would get the value
- 'SPELLCASTER RU', that is only 14 chars long...
-
- - There's a limit of memory the Pascal reserves for variables, so you can't
- define an infinite number of variables. This limit is for now of 64 Kb, but
- don't worry about it by now, because you won't make variables that can ocupy
- that space. So that you know, a var of type integer ocuppies 2 bytes ! So,
- you can store 32768 integer vars in memory !
-
- Next there is examples of the operations you can do with ordinal type of
- variables, which are Shortint, Integer, Longint, Byte and Word. I'll use
- just words, but it's the same for all the ordinal types of variables.
- This isn't a program... It's just some examples:
-
- Var A,B:Word; { Defines variables A and B as Words }
-
- A:=10; B:=15; { You can put multiple commands in one line, as long as they }
- { separeted by a semi-colon. }
- A:=A*B; { Multiplies A by B and stores result in A }
- A:=A DIV B; { Divides A by B and stores result in A }
- A:=A+B; { Adds A to B and stores result in A }
- A:=A-B; { Subtracts B from A and stores result in A }
- B:=A MOD B; { Divides A by B and stores the modulo in B }
- A:=A+1; { Adds one to A and stores result in A }
- B:=A+42; { Adds 42 to A and stores result in B }
- B:=200-A; { Subtracts A from 200 and stores result in B }
- B:=A+B*2; { Multiplies B by 2, adds that to A and stores result in B }
-
- I think this cover the main operations. Don't forget the following points:
-
- - The DIV operation gives the integer result of the division, that is, if
- you divide 15 by 2, you will get 7. The decimal part is ignored.
-
- - The order in which operation are made are equal to the standart order in
- normal maths, except that the computer calculates from right to left,
- while normal maths do operations from left to right. The order can be
- altered by using parentesis. So:
-
- A:=A+B*2; { Multiplies B by 2, adds that to A and stores result in A }
- A:=(A+B)*2; { Adds A to B, multiplies that by 2 and stores result in A }
-
-
- Now, let's cover the string maths... this is quite unusual, because, normaly
- you can only add numbers. Though it's strange, it's simple, as there is only
- one operation (by now)...
-
- Var A,B:String; { Defines A and B as strings }
-
- A:='Spell'; { Gives A the value 'Spell' }
- B:='caster'; { Gives B the value 'caster' }
-
- A:=A+B; { Adds A to B and stores result in A; A='Spellcaster' }
- A:=A+' Rules';{ Adds A to ' Rules' and stores result in A, }
- { so A='Spellcaster Rules' }
-
-
- Simple isn't it ? Keep practicing and you'll reach perfection. This is quite
- easy, but, for a beginner it can be complicated, because it's a new concept.
-
- ■ The WRITELN and READLN keywords
-
- These are one of the more helpfull commands for beginners. Basically, what
- they do is Write and Read... Tough, isn't it ?!
-
- Ok, let's examine WRITELN keyword.
- It's sintax (sintax is how you call the command) is:
-
- WRITELN (what to write);
-
- The 'what to write' in parentesis is just that... It's what you want to
- write. It can be almost anything you want. If you don't write nothing, he
- just skips a line. Let's go to an example:
-
- Program Test_3 { Defines program Test_3 }
-
- Var A:String; { Defines variable A as a string }
- B:Word; { Defines variable B as a word }
-
- Begin { Begins the main block }
- A:='Spellcaster'; { Gives var A the value 'Spellcaster' }
- B:=1995; { Gives var B the value 1995 }
- Writeln(A); { Writes the content of variable A }
- Writeln(B); { Writes the content of variable B }
- Writeln('We are in ',B); { Writes 'We are in ', followed by var B }
- Readln; { Waits for RETURN to be pressed }
- End. { Ends the program }
-
- Now, try and change the WRITELN's in the program for WRITE's only (yes,
- that's right... No LN ! ). If you do this, you will notice that the word
- 'Spellcaster' will appear followed imediattly by the number 1995. The
- diference between WRITELN and WRITE is that WRITELN changes line after
- writing.
- WRITELN can also be used to write to the printer, but I'll save that for
- a future issue.
-
- Say kids, what time is it ?
- It's time to move on to READLN.
-
- We have just seen one of the uses of READLN in previous programs. It can be
- used to wait for a person to press RETURN, but that's just the tip of the
- iceberg... As you may have noticed (if you executed the above shown programs)
- is that if you press any key besides RETURN, while the program is waiting, is
- that the text you type in appears on the screen. This is a very usefull thing
- and I'm going to show you why:
-
- Program Test_4; { This starts program Test_4 }
-
- Var A:String; { Defines a string called A }
-
- Begin { Starts main block }
- A:='Spellcaster'; { Gives var A the value 'Spellcaster' }
- Writeln(A); { Writes the content of var A on the screen }
- Readln(A); { Reads a new value for variable A... }
- Writeln(A); { ...and writes it in the screen. }
- Readln; { Waits for RETURN to be pressed }
- End. { Ends program }
-
- See why READLN is so usefull ?! It gives you the hability to read the
- value of a variable from the keyboard.
- So, now you know somethings to keep you entertained until the next issue.
- Play with the programs, try to understand it and, especially, try to do a
- program alone... You can do it with some patience. It's not that hard...
- I will leave this section with one last program. I won't do what it does, so
- you'll have to type it in. The only thing I say is that this program uses a
- lot of the things we talked today. I won't comment it on purpose, to make you
- read it to understand it.
-
- Program Maths(Input,Output);
-
- Var A,B:Integer;
- C:Integer;
-
- Begin
- Writeln('The Maths Version 1.0');
- writeln;
- write('Type in number A : ');
- readln(A);
- write('Type in number B : ');
- readln(B);
- C:=A+B;
- Writeln('A+B=',C);
- C:=A-B;
- Writeln('A-B=',C);
- C:=A*B;
- Writeln('A*B=',C);
- C:=A DIV B;
- Writeln('A DIV B=',C);
- C:=A MOD B;
- Writeln('A MOD B=',C);
- Readln;
- End.
-
-
-
- ■ Our friend, the pointer - Part I
-
- This article is aimed to those of you who already know some programming.
- If you are a beginner, skip this article, get some more issues of 'THE MAG',
- learn what you can with it, and then return to this issue.
-
- ■ What the hell is a pointer ?
-
- A pointer, as the name sugests is a type of variable that points to
- something. That something it's usually a position in memory.
-
- ■ What's the use ?
-
- Imagine you want to create a array of some kind, let's say a 320 by
- 200 array of type byte. Normally you would do like
- this:
- Var A:Array[1..320,1..200] of byte;
- If you would try to compile a program with a line like this, you would,
- but you finished up beeing without space for the other variables in the
- program, because, as you may know, Pascal has a 64Kb limit for variables.
- What's the solution for this? Pointers, of course !!!
-
- ■ How can I use the pointer ?
-
- There's three ways of using a pointer: two of them are dynamic (read by
- that harder, because they don't have a fixed size) and one not-dynamic.
- I'll teach you the not-dynamic one, because it's very easy. I'll save the
- dynamic solution some issues away from now.
- Normally, if Pascal had a bigger than 64Kb limit for variables, you could
- a program like this (the dots are other code that doesn't matter):
-
- Program Test_5;
-
- Var A:Array[1..320,1..200] of byte;
- B:Array[1..320,1..200] of byte;
-
- Begin
- .......
- .......
- .......
- .......
- A[221,2]:=1;
- B[300,43]:=2;
- .......
- Write(A[221,2]);
- Write(B[300,43]);
- .......
- .......
- .......
- End.
-
- This wouldn't compile, because it excedes the 64Kb limit.
- To do this in Pascal, with the 64Kb limit, you must do it like this:
-
- Program Test_5;
-
- Type C=Array[1..320,1..200] of byte;
-
- Var A:^C;
- B:^C;
-
- Begin
- Getmem(A,Sizeof(A^));
- Getmem(B,Sizeof(B^));
- .......
- .......
- .......
- A^[221,2]:=1;
- B^[300,43]:=2;
- .......
- Write(A^[221,2]);
- Write(B^[300,43]);
- .......
- .......
- .......
- Freemem(A,Sizeof(A^));
- Freemem(B,Sizeof(B^));
- End.
-
- This second program would work because you would be spending base-memory
- of the computer, and not the 64Kb Pascal has for variables.
- Now, for the hard part: explaining how this works.
- The TYPE clause in the start is necessary, because who need to point to
- something when we define the A and B variables. You place there wherever you
- want to allocate in base memory. The VAR clause says that vars A and B point
- to an object with the structure of type C.
- Then, you must allocate the memory for the pointer. So, you use the Getmem
- keyword. The sintax for Getmem is:
-
- Getmem (pointer, size);
-
- So, you grab the memory you need. Then, as you are working with pointers,
- instead of refering to the variables as A[10,10], you refer as A^[10,10].
- Easy, isn't it ?... Next, in the end of the program (or when you want to free
- the memory for another pointer or something like that), you use the Freemem
- keyword. The sintax is:
-
- Freemem (pointer, size);
-
- You must de-allocate as much memory as you allocate, under the penalty of
- losing that memory. The problem with working with pointers is that if you
- don't de-allocate them, after a couple of program execution, you'll get an
- 'Out of Memory' error, because the memory is already filled with pointers.
- So, never forget to de-allocate the pointers.
- As you can see, this method of using pointers is very easy to use and
- implement. The other methods (the dynamic ones) are ten times more
- complicated, and in the majoraty of cases, this is better, especially for
- games and demos, because everything in them is planned.
- Keep one thing in mind, tough. Because of the way PC's work, you can not
- allocate more than 64Kb to just one pointer. This has to do with
- segmentation, but there are ways to turn this around.
- One more thing, in the lines with Getmem and Freemem I use a function,
- called sizeof, whose sintax is:
-
- =sizeof(structure);
-
- This function returns the size in bytes of the structure in parentesis.
-
-
-
- ■ Points of view
-
- Although this article is called Points of View, it has nothing to do with
- oppinion. This is like the conclusion of the magazine. I just called that
- because I will sometimes expose my distorted points of view here, and I didn't
- wanted to call this 'Conclusion'... It would sound vulgar.
-
- So, what will I see here? I don't really know. After all, this is the first
- issue. I don't have nothing to say. I'll wait until I read/hear some remarks
- about 'The Mag', and then I'll write something, like a response or an
- apologie request.
-
- As I talked about apologies, I will take the chance to ask one or two.
- I apologize for the English in this magazine, but you must understand three
- things:
- 1. I'm not English
- 2. It's bloody hard keeping a good thinking without worring about the
- the grammar, left alone if I cared.
- 3. I write very fast (compared to the other persons), and at that speed
- it's very hard not to make mistakes.
- I also apologize about my language and the whole tone of the magazine. In this
- case, you must notice I don't get any money for doing this, so I must do it
- for fun.
-
- Well, there's only one thing left in my magazine...
-
- ■ The adventures of Spellcaster, the rebel programmer of the year 2018.
-
- Episode 1 - The beggining
-
- I remember it all... From the day it started my trail of misery... And I
- think I'll never forget it.
- It was a friday night, the 26th September 2018. I was just trying to program
- my new computer I got by my 18th birthday, just 23 days from then. It was a
- top of line Octium, clocked at 33 Gigahertz, with 10 Gigabytes RAM, a hard
- drive with 800 Gigabytes hard drive and a whole lot more stuff. Just what I
- always wanted. As I said, I was programming my computer, a simple strategy
- game in Pascal v18.2, when suddently, my computer ran dead. It wouldn't do
- nothing. It just froze up. I tried rebooting it, then I turned the power off
- and on, and nothing would happen. In panic, I rushed to the phone, and I
- started dialing the number of COMPTEL, the largest manufacturer of computers,
- programs and other assorted things. It also fixed computers. They said they
- were on the way. I sat down, a bit more relaxed and started to think about
- COMPTEL, from it's founding by some guy called Bill something to their
- takeover of the world. I was disturbed from my toughts by a siren. I look
- out the window, and there it was... A white van with the words 'COMPTEL'
- writen in red on the side. They knocked at my door. I opened the door, and
- two persons entered. They were exactly alike, quite tall, brown hair, glasses
- and bad breath. As I live in the age of clones, I wasn't surprised. I showed
- them the computer and they told me to leave. So I did. Minutes later, they
- called me. I entered my room, anxious. They weren't looking happy, but I
- didn't cared. All I cared was my computer. I looked at it, and the green
- letters the screen displayed just made my fears away.
- - Thank you very much... - I said, with a big smile.
- - Mr. Andrade... As we always do after a repair, we scan the disk for pirated
- software, subversive campaign against COMPTEL, that kind of things.
- - What do you mean ?... I don't have any of those.
- - Yes, that is true. - said the other man. - You have very little software
- instaled in your hard drive indeed.
- - But we've noticed a very serious fellony...
- - Can you tell us, Mr.Andrade, why isn't Windows 2018 installed ?
- - Because I don't have any use for it... I am a programmer, not a user. I
- pratically I only use MY-DOS, v6.0. - I answered, my heart going faster.
- - How can you program without Windows ? - one of them asked, angry.
- - Without Visual Basic ? - asked the other, with a yellow smile.
- - Yes... - I answeared, nervously. - I use Turbo Pascal... With assembler.
- - Do you know article 52421/52 of COMPTEL law, Mr.Andrade ?
- - N-n-n-no I don't. - I began to sweat.
- - The article states that no operating sistem besides Windows 2018 can be
- used... The article also says that no programming language, other than
- Visual Basic, can be used.
- - But...but... I'm a programmer. Windows and Visual Basic aren't made for
- programmers...
- - Tough luck... - said one of the COMPTEL men, as a third man grabed me from
- behind, dragging me to the van, while my parents looked as I was beeing
- taken away.
- A week after, I was in court, before The Judge himself... Bill Gates Jr.
- - How do you plea to this charges ?
- - Not guilty, sir...
- - Ha, ha, ha !!! - the whole room laughed, as The Judge hitted the table with
- his hammer.
- - Diogo de Andrade, I found you guilty of violating the article 52421/52,
- usage of illegal software. The sentence will be 30 years incarceration,
- with no possibility of parole. The sentence will be carried out in the
- Atari World Penetenciary...
- - No, no... - I screamed. - Everything but the Atari Penetenciary.
- The judge laughed at my plea, and ignored it.
- Now, these walls is all I have left. I am here at almost a month now, with
- no computers to program, except for Ataris... Cruel faith is mine. Beeing
- doomed to 30 years of this suffering. Unless, of course, I can escape...
-
-
-
- See you in the next issue
- Diogo "SpellCaster" Andrade
-